package com.team.game.Game;

import android.graphics.PointF;
import android.graphics.RectF;
import android.util.Log;

public class Impacts {

    //точки для impactOnDegRect(...)
    private static PointF points[] = new PointF[4];

    public static void impactOnTheRadius (Levels level, PointF pointF, float Damage, float Radius) {
        for (int i = 0; i < level.Num_Wave; i++) {

            if (!(level.Time > level.Time_Wave_Start[i] && level.Time < level.Time_Wave_End[i]))
                continue;

            //---из вейва
            for (int j = 0; j < level.arrayListMob.get (i).Num_Mob_Wave; j++) {

                if (level.arrayListMob.get (i).Sprites.get (j).deadSearch) //перевірка: чи не було вбито моба
                    continue;

                float distance = distance (level.arrayListMob.get (i).Sprites.get (j).getRect ().centerX (), pointF.x,
                        level.arrayListMob.get (i).Sprites.get (j).getRect ().centerY (), pointF.y);
                if (distance < Radius) {
                    level.arrayListMob.get (i).Sprites.get (j).dec (Damage);
                }
            }
            //---
        }
    }

    public static void impactOnDegRect (Levels level, RectF posOfBmp, float alpha, PointF pnt, float damage) {
        for (int i = 0; i < level.Num_Wave; i++) {

            if (!(level.Time > level.Time_Wave_Start[i] && level.Time < level.Time_Wave_End[i]))
                continue;

            //---из вейва
            alpha = (float) Math.toRadians (alpha);
            for (int j = 0; j < level.arrayListMob.get (i).Num_Mob_Wave; j++) {

                if (level.arrayListMob.get (i).Sprites.get (j).deadSearch) //перевірка: чи не було вбито моба
                    continue;

                level.arrayListMob.get (i).bool = true;

                points[0] = new PointF (posOfBmp.left, posOfBmp.top);
                points[1] = new PointF (posOfBmp.right, posOfBmp.top);
                points[2] = new PointF (posOfBmp.left, posOfBmp.bottom);
                points[3] = new PointF (posOfBmp.right, posOfBmp.bottom);

                for (int k = 0; k < 4; k++) {

                    points[k].x = (float) ((points[k].x - pnt.x) * Math.cos (alpha) - (points[k].y - pnt.y) * Math.sin (alpha) + pnt.x);
                    points[k].y = (float) ((points[k].x - pnt.x) * Math.sin (alpha) + (points[k].y - pnt.y) * Math.cos (alpha) + pnt.y);

                }

                PointF point = new PointF (level.arrayListMob.get (i).Sprites.get (j).getRect ().centerX (), level.arrayListMob.get (i).Sprites.get (j).getRect ().centerY ());

                float a = (points[0].x - point.x) * (points[1].y - points[0].y) - (points[1].x - points[0].x) * (points[0].y - point.y);
                float b = (points[1].x - point.x) * (points[2].y - points[1].y) - (points[2].x - points[1].x) * (points[1].y - point.y);
                float c = (points[2].x - point.x) * (points[0].y - points[2].y) - (points[0].x - points[2].x) * (points[2].y - point.y);

                float d = (points[3].x - point.x) * (points[1].y - points[3].y) - (points[1].x - points[3].x) * (points[3].y - point.y);
                float e = (points[2].x - point.x) * (points[3].y - points[2].y) - (points[3].x - points[2].x) * (points[2].y - point.y);


                if (((a >= 0 && b >= 0 && c >= 0) || (d >= 0 && e >= 0 && b >= 0)) ||
                        ((a <= 0 && b <= 0 && c <= 0) || (d <= 0 && e <= 0 && b <= 0))) {
                    Log.i ("RAY", "kick to : " + point.toString ());
                    level.arrayListMob.get (i).Sprites.get (j).dec (damage);
                }

                //---
            }

        }
    }


    public static float distance (float rectCenterX, float x, float rectCenterY, float y) {
        return (float) (Math.sqrt (Math.pow ((rectCenterX - x), 2.0) + Math.pow (rectCenterY - y, 2.0)));
    }


}
